#!/usr/bin/env python

import sys
import urllib2
import re

def getAlternateIsbns(isbn):
    data = urllib2.urlopen('http://old-xisbn.oclc.org/webservices/xisbn/' + isbn).read()
    return re.findall('<isbn>([^<]+)</isbn>', data)

def getAllConsumingNumber(isbn):
    data = urllib2.urlopen(' http://www.allconsuming.net/item/asin/' + isbn).read()
    return re.findall('/item/view/([0-9]+)', data)[0]

def getIsbn(acn):
    data = urllib2.urlopen(' http://www.allconsuming.net/item/view/' + acn).read()
    return re.findall('http://www.amazon.com/o/asin/([0-9]+)', data)[0]
    
    
def main(args=None):
    if args == None:
        args = sys.argv[1:]

    acns = []
    acn = args[0]
    isbn = getIsbn(acn)
    isbns = getAlternateIsbns(isbn)
    for newIsbn in isbns:
        try:
            acns.append(getAllConsumingNumber(newIsbn))
        except:
            pass

    print acns
                  
    
#     isbn = args[0]
#     isbns = getAlternateIsbns(isbn)

#     for foundIsbn in isbns:
#         print getAllConsumingNumber(foundIsbn)
#     return 0


if __name__ == '__main__':
    sys.exit(main())

